/*
 * @Author: heinan
 * @Date: 2020-07-16 11:33:19
 * @Last Modified by: wangshuihong
 * @Last Modified time: 2023-08-09 20:35:56
 */
"use strict";
const { Service } = require("egg");
const { idCreator } = require("../utils");

class BrokerService extends Service {
  //获取经纪人信息
  async index({ name, status, currentPage, pageSize }) {
    const where = {};
    if (name) {
      where.name = name;
    }
    if (status == "请选择") {
    } else if (status == "在职") {
      where.status = 1;
    } else if (status == "离职") {
      where.status = 0;
    } else {
    }
    return await this.app.mysql.select("brokers", {
      where,
      offset: (currentPage - 1) * pageSize,
      limit: parseInt(pageSize),
    });
  }
  //删除经纪人信息
  async destroy({ id }) {
    return await this.app.mysql.delete("brokers", { id });
  }
  //修改经纪人离职在职状态
  async update({
    id,
    status,
    company,
    name,
    first_hand_house,
    second_hand_house,
    mobile,
    lease_house,
  }) {
    if (company) {
      const $data = {
        id,
        status,
        company,
        name,
        first_hand_house,
        second_hand_house,
        mobile,
        lease_house,
      };
      return await this.app.mysql.update("brokers", $data);
    } else {
      const $data = { id, status };
      console.log(id, status);
      return await this.app.mysql.update("brokers", $data);
    }
  }
  //添加经纪人
  async create({
    name,
    mobile,
    company,
    first_hand_house,
    second_hand_house,
    lease_house,
    status,
  }) {
    const $data = {
      name,
      mobile,
      company,
      first_hand_house,
      second_hand_house,
      lease_house,
      status,
      id: idCreator(),
    };
    console.log($data.id, "78978979789798");
    return await this.app.mysql.insert("brokers", $data);
  }
}

module.exports = BrokerService;
